package com.bcts.rrh.drawingboard.shapeview;

import android.graphics.Color;
import android.opengl.Matrix;
import android.util.Log;

import com.bcts.rrh.drawingboard.utils.VaryTools;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;

import javax.microedition.khronos.opengles.GL10;

import static com.bcts.rrh.drawingboard.Constants.TOUCH_SCALE_FACTOR;

public class Parallelogram extends Shape_3d{
    private FloatBuffer mVertexBuffer;//顶点坐标数据缓冲
    float yAngle=0;//绕y轴旋转的角度
    float mAnglePi=0;
    float x1;float y1;
    float x2;float y2;
    float x3;float y3;
    float x4;float y4;
    int vCount=0;//顶点数量
    int color;
    float scalezoom=1.0f;
    float[] tempVerteices;
    ArrayList<Float> inittempVerteices;//初始的坐标点数组
    float minX;//x轴最小位置
    float maxX;//x轴最大位置
    float minY;//y轴最小位置
    float maxY;//y轴最大位置
    float minZ;//z轴最小位置
    float maxZ;//z轴最大位置
    float midX;//中心点坐标
    float midY;
    float midZ;
    float xOffset;//在轴上移动的位置
    float yOffset;
    float zOffset;


    public Parallelogram(float x1, float y1,int color,float xOffset,float yOffset,float zOffset){
        this.x1=x1;this.x2=x1+0.6f;this.x3=x1-0.1f;this.x4=x1+0.5f;
        this.y1=y1;this.y2=y1;this.y3=y1+0.2f;this.y4=y1+0.2f;
        this.color=color;
        this.xOffset=xOffset;
        this.yOffset=yOffset;
        this.zOffset=zOffset;
        vCount=6;//设定顶点数量
        float[] vertices=new float[]{//创建顶点数据数组
                x1,y1,0f,
                x2,y2,0f,
                x3,y3,0f,

                x2,y2,0f,
                x3,y3,0f,
                x4,y4,0f
        };
        tempVerteices=vertices;
        //创建顶点坐标数据缓存，由于不同平台字节顺序不同，数据单元不是字节的要经ByteBuffer转换
        ByteBuffer vbb=ByteBuffer.allocateDirect(vertices.length*4);//开辟新的内存块
        vbb.order(ByteOrder.nativeOrder());//设置为本地平台的字节顺序
        mVertexBuffer=vbb.asFloatBuffer();//转换为int型缓冲
        mVertexBuffer.put(vertices);//向缓冲区中放入顶点坐标数据
        mVertexBuffer.position(0);//设置缓冲区的起始位置
    }
    //点缓存的坐标变换，按矩阵变换来变换
    private float[] pointMatrix(){
        //1.计算坐标变换矩阵
        VaryTools tools=new VaryTools();
        tools.pushMatrix();
        //tools.translate(xOffset, yOffset, zOffset);
        tools.rotate(yAngle,0,0,-1);
        tools.scale(scalezoom, scalezoom, scalezoom);
        float[] mMatrixCurrent=tools.getmMatrixCurrent();
        tools.popMatrix();
        float vertices[]=new float[inittempVerteices.size()];
        for(int i=0;i<inittempVerteices.size();i++)
        {
            vertices[i]=inittempVerteices.get(i);
        }
        //float[] tempVerteices=inittempVerteices;//将点坐标变换为初始状态的坐标重新计算
        for(int i=0;i<vertices.length/3;i++){
            float[] pointMx={vertices[i*3],vertices[i*3+1],vertices[i*3+2],1};
            Matrix.multiplyMV(pointMx,0,mMatrixCurrent,0,pointMx,0);
            vertices[i*3]=pointMx[0];
            vertices[i*3+1]=pointMx[1];
            vertices[i*3+2]=pointMx[2];
        }
        return vertices;
    }
    public void resetBuffer(){
        vCount=6;//设定顶点数量
        float[] vertices=new float[]{//创建顶点数据数组
                x1,y1,0f,
                x2,y2,0f,
                x3,y3,0f,

                x2,y2,0f,
                x3,y3,0f,
                x4,y4,0f
        };
        tempVerteices=vertices;
        //创建顶点坐标数据缓存，由于不同平台字节顺序不同，数据单元不是字节的要经ByteBuffer转换
        ByteBuffer vbb=ByteBuffer.allocateDirect(vertices.length*4);//开辟新的内存块
        vbb.order(ByteOrder.nativeOrder());//设置为本地平台的字节顺序
        mVertexBuffer=vbb.asFloatBuffer();//转换为int型缓冲
        mVertexBuffer.put(vertices);//向缓冲区中放入顶点坐标数据
        mVertexBuffer.position(0);//设置缓冲区的起始位置
    }
    private void test(){
        //创建绘制顶点数据缓冲
        ByteBuffer vbb = ByteBuffer.allocateDirect(tempVerteices.length*4);
        vbb.order(ByteOrder.nativeOrder());//设置字节顺序
        //mVertexBuffer.clear();
        mVertexBuffer = vbb.asFloatBuffer();//转换为float型缓冲
        mVertexBuffer.put(tempVerteices);//向缓冲区中放入顶点坐标数据
        mVertexBuffer.position(0);//设置缓冲区起始位置
    }
    public void drawSelf(GL10 gl){//实现具体的绘制方法
        gl.glTranslatef(xOffset, yOffset, zOffset);
        //gl.glScalef(scalezoom, scalezoom, scalezoom);//对三角形进行缩放
        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);//启用顶点坐标数组
        //gl.glRotatef(yAngle,0,0,-1);//根据yAngle的角度值，绕y轴旋转yAngle
        gl.glVertexPointer(//为画笔指定顶点坐标数据
                3,//每个顶点的坐标数量为3
                GL10.GL_FLOAT,//顶点坐标值的类型为GL_FIXED,整型
                0,//连续顶点坐标数据直接的间隔
                mVertexBuffer//顶点坐标来源
        );
        int red=Color.red(color);
        int green=Color.green(color);
        int blue=Color.blue(color);
        int alpha=Color.alpha(color);
        gl.glEnable(GL10.GL_POLYGON_OFFSET_FILL);
        gl.glPolygonOffset(5.0f,5.0f);
        gl.glColor4f(red/255.0f,green/255.0f,blue/255.0f, alpha/255.0f);
        gl.glDrawArrays
                (
                        //GL10.GL_LINE_LOOP, 		//以三角形方式填充
                        GL10.GL_TRIANGLES, 		//以三角形方式填充
                        0, 			 			//开始点编号
                        vCount					//顶点数量
                );
    }

    @Override
    public float[] findMinMax() {
        // TODO Auto-generated method stub
        //最大最小值初始化
        minX=tempVerteices[0];//x轴最小位置
        maxX=tempVerteices[0];//x轴最大位置
        minY=tempVerteices[1];//y轴最小位置
        maxY=tempVerteices[1];//y轴最大位置
        minZ=tempVerteices[2];//z轴最小位置
        maxZ=tempVerteices[2];//z轴最大位置
        for(int i=0;i<tempVerteices.length/3;i++)
        {
            //判断X轴的最小和最大位置
            if(tempVerteices[i*3]<minX)
            {
                minX=tempVerteices[i*3];
            }
            if(tempVerteices[i*3]>maxX)
            {
                maxX=tempVerteices[i*3];
            }
            //判断Y轴的最小和最大位置
            if(tempVerteices[i*3+1]<minY)
            {
                minY=tempVerteices[i*3+1];
            }
            if(tempVerteices[i*3+1]>maxY)
            {
                maxY=tempVerteices[i*3+1];
            }
            //判断Z轴的最小和最大位置
            if(tempVerteices[i*3+2]<minZ)
            {
                minZ=tempVerteices[i*3+2];
            }
            if(tempVerteices[i*3+2]>maxZ)
            {
                maxZ=tempVerteices[i*3+2];
            }
        }
        Log.d("Ball minX="+minX,"maxX="+(maxX-minX));
        Log.d("Ball minY="+minY,"maxY="+(maxY-minY));
        Log.d("Ball minZ="+minZ,"maxZ="+(maxZ-minZ));
        float[] length={maxX-minX,maxZ-minZ,maxY-minY};

        return length;
    }

    @Override
    public float[] findMid() {
        // TODO Auto-generated method stub
        midX=(minX+maxX)/2+xOffset;
        midY=(minY+maxY)/2+yOffset;
        midZ=(minZ+maxZ)/2+zOffset;
        Log.d("Ball midX=",""+midX);
        Log.d("Ball midY=",""+midY);
        Log.d("Ball midZ=",""+midZ);
        float[] mid={midX,midY,midZ};
        return mid;
    }

    public void setMove(boolean flag) {
        this.moveFlag=flag;
        Log.d("cube",""+this.moveFlag);
    }
    public void setRotate(boolean flag) {//移动
        this.rotateFlag=flag;
        Log.d("cube",""+this.rotateFlag);
    }

    @Override
    public void setHilight(boolean flag) {
        this.hiFlag=flag;
        Log.d("cube",""+this.hiFlag);
    }

    @Override
    public void settexture(int texId) {
        /*if(texId>0){//如果纹理存在
            this.texId=texId;
        }*/
    }

    @Override
    public boolean getMove() {
        return this.moveFlag;
    }

    @Override
    public boolean getRotate() {
        return this.rotateFlag;
    }

    @Override
    public boolean getHilight() {
        return this.hiFlag;
    }

    @Override
    public void Move(float dx, float dy) {
        xOffset=dx;
        yOffset=dy;
    }

    @Override
    public void Rotate(float dx, float dy,float ox,float oy) {
        updateRotate(dx,dy,ox,oy);
        tempVerteices=pointMatrix();
        test();
    }
    //endX,endY是手指在屏幕上的位移量，ox,oy触点当前的3D坐标
    private void updateRotate(double endX,double endY,double ox,double oy) {
        //因为安卓屏幕的坐标原点在左上角，故endy反方向
        //float dr=(float) Math.sqrt(Math.pow((ox-xOffset),2)+Math.pow((oy-yOffset),2));//旋转半径
        float dm=(float) Math.sqrt(Math.pow(endX,2)+Math.pow(endY,2));//旋转位移量
        System.out.println("endX="+endX+",endY="+endY+",ox="+ox+",oy="+oy);
        if (xOffset < ox && yOffset>= oy) {// 第一象限
            //手指往往下或者往左顺时针转
            if(endX>0 || endY<0) {
                if(endX<0){
                    if(endX-endY<0)yAngle += TOUCH_SCALE_FACTOR*dm*0.5;
                }
                if(endY>0){
                    if(endX-endY<0)yAngle+= TOUCH_SCALE_FACTOR*dm*0.5;
                }
                yAngle -= TOUCH_SCALE_FACTOR*dm*0.5;
            }
            //否则逆时针转
            else yAngle += TOUCH_SCALE_FACTOR*dm*0.5;//旋转角度的增值

        } else if (xOffset >= ox && yOffset > oy) {// 第二象限
            if(endX>0 || endY>0) {
                if(endX<0){
                    if(endX-endY<0)yAngle += TOUCH_SCALE_FACTOR*dm*0.5;
                }
                if(endY<0){
                    if(endX+endY<0)yAngle += TOUCH_SCALE_FACTOR*dm*0.5;
                }
                yAngle -= TOUCH_SCALE_FACTOR*dm*0.5;
            }
            else yAngle += TOUCH_SCALE_FACTOR*dm*0.5;

        } else if (xOffset > ox && yOffset <= oy) {// 第三象限
            if(endX<0 || endY>0){
                if(endX>0){
                    if(endX-endY>0)yAngle += TOUCH_SCALE_FACTOR*dm*0.5;
                }
                if(endY<0){
                    if(endX-endY>0)yAngle += TOUCH_SCALE_FACTOR*dm*0.5;
                }
                yAngle -= TOUCH_SCALE_FACTOR*dm*0.5;
            }
            else  yAngle += TOUCH_SCALE_FACTOR*dm*0.5;

        }else {
            if(endX<0 || endY<0) {
                if(endX>0){
                    if(endX+endY>0)yAngle += TOUCH_SCALE_FACTOR*dm*0.5;
                }
                if(endY>0){
                    if(endX+endY>0)yAngle += TOUCH_SCALE_FACTOR*dm*0.5;
                }
                yAngle -= TOUCH_SCALE_FACTOR*dm*0.5;
            }
            else yAngle += TOUCH_SCALE_FACTOR*dm*0.5;
        }
    }
    @Override
    public void Hilight(float scachange) {
        if(scachange>0){
            this.scalezoom+=0.01f;
        }
        else {
            if(this.scalezoom>=0.2f){
                this.scalezoom-=0.01f;
            }
        }
        tempVerteices=pointMatrix();
        test();
    }

    @Override
    public void setpoint(int pointid, float dx, float dy) {
        x4=dx-xOffset;y4=dy-yOffset;
        y3=dy-yOffset;x3=dx-0.6f-xOffset;
        resetBuffer();
    }

    @Override
    public void setcolor(int color) {
        this.color=color;
    }

    @Override
    public void meActionup() {
        inittempVerteices=new ArrayList<Float>();//存放顶点坐标的ArrayList
        for (int i=0;i<tempVerteices.length;i++){
            inittempVerteices.add(tempVerteices[i]);
        }
        tempVerteices=pointMatrix();
    }
}
